/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.up.kafka.sender;


import com.up.kafka.productor.KafkaProducer;
import com.up.kafka.productor.MessageRecord;
import com.up.kafka.productor.MsgCallBack;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @author: baiMl
 * @title: BasicSender
 * @projectName: 海联技术中台V2.0
 * @depart 软件事业部
 * @description:
 * @date: 2022/4/25 8:44
 */
@SuppressWarnings("unchecked")
public abstract class BasicSender implements MessageSender {

    @SuppressWarnings("rawtypes")
    private final KafkaProducer producer;

    public BasicSender(@SuppressWarnings("rawtypes") KafkaProducer producer) {
        this.producer = producer;
    }

    /**
     * 包装消息
     *
     * @param key       关键KEY
     * @param message   原始消息对象
     * @param topic     消息主题
     * @param partition 分区
     * @param <V>       消息类型
     * @return 返回包装的MessageRecord对象
     * @throws IOException IO异常
     */
    protected abstract <V> MessageRecord<Object> wrapperMsg(String key, V message, String topic, Integer partition) throws IOException;


    @Override
    public <V> void sendOnce(String key, V message, String topic) throws IOException {
        producer.sendOnce(wrapperMsg(key, message, topic, null));
    }

    @Override
    public <V> void sendOnce(String key, V message, String topic, Integer partition) throws IOException {
        producer.sendOnce(wrapperMsg(key, message, topic, partition));
    }

    @Override
    public <V> Object sendSync(String key, V message, String topic, Integer partition) throws ExecutionException, InterruptedException, IOException {
        return producer.sendSync(wrapperMsg(key, message, topic, partition));
    }

    @Override
    public <V> Object sendSync(String key, V message, String topic) throws ExecutionException, InterruptedException, IOException {
        return producer.sendSync(wrapperMsg(key, message, topic, null));
    }

    @Override
    public <V> Object sendSync(String key, V message, String topic, long time, TimeUnit timeUnit) throws ExecutionException, InterruptedException, TimeoutException, IOException {
        return producer.sendSync(wrapperMsg(key, message, topic, null), time, timeUnit);
    }

    @Override
    public <V> Object sendSync(String key, V message, String topic, Integer partition, long time, TimeUnit timeUnit) throws ExecutionException, InterruptedException, TimeoutException, IOException {
        return producer.sendSync(wrapperMsg(key, message, topic, partition), time, timeUnit);
    }

    @Override
    public <V> void sendCallBack(String key, V message, String topic, MsgCallBack<V> callBack) throws IOException {
        producer.sendAndCallBack(wrapperMsg(key, message, topic, null), callBack);
    }

    @Override
    public <V> void sendCallBack(String key, V message, String topic, Integer partition, MsgCallBack<V> callBack) throws IOException {
        producer.sendAndCallBack(wrapperMsg(key, message, topic, partition), callBack);
    }
}
